﻿Public Class frmEmployeeInfoBR
    Private frmParent As frmMain
    Dim validator As PropertyValidator = New PropertyValidator
    Dim empMgr As New EmployeeManager
    Dim empInfoMgr As New EmployeeInfoManager
    Dim bioMgr As New BiometricsManager
    Dim pMgr As New PayrollManager
    Dim EmployeeInfo As EmployeeInfo

    Private Sub frmEmployeeInfoBR_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
    End Sub

    Private Sub frmEmployeeInfoBR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MdiParent = frmMain
        ClearAllFields()

       

        Dim employeeId As String = Clipboard.GetData(DataFormats.Text)
        If employeeId <> "" Then
            Try

                Dim fpCount As Integer = bioMgr.retrieveFingerPrintCountByEmployeeId(Clipboard.GetData(DataFormats.Text))
                lblFPCount.Text = fpCount.ToString

                EmployeeInfo = empInfoMgr.retrieveEmployeeInfoByEmployeeId(Clipboard.GetData(DataFormats.Text))
                If Not EmployeeInfo Is Nothing Then
                    Dim item As ListViewItem
                    item = lvwEmployeeInfo.Items.Add(EmployeeInfo.id)
                    item.SubItems.Add(EmployeeInfo.employee_id)
                    item.SubItems.Add(EmployeeInfo.employee_no)
                    item.SubItems.Add(EmployeeInfo.last_name)
                    item.SubItems.Add(EmployeeInfo.first_name)
                    item.SubItems.Add(EmployeeInfo.middle_name)
                    item.SubItems.Add(EmployeeInfo.no_sss)
                    item.SubItems.Add(EmployeeInfo.no_philhealth)
                    item.SubItems.Add(EmployeeInfo.no_pagibig)
                    item.SubItems.Add(EmployeeInfo.no_tin)

                    Dim taxCodeString As String = ""
                    Dim tc As List(Of TaxCode) = pMgr.getItemTaxCodeByID(EmployeeInfo.tax_code)
                    If Not tc Is Nothing Then
                        For Each tcIndiv As TaxCode In tc
                            taxCodeString = tcIndiv.tax_code
                            Exit For
                        Next
                    Else
                    End If
                    item.SubItems.Add(taxCodeString)

                    Dim dMgr As New DesignationManager
                    Dim designation As Designation = dMgr.retrieveDesignationById(EmployeeInfo.designation_id)
                    If Not designation Is Nothing Then item.SubItems.Add(designation.title)

                    item.SubItems.Add(EmployeeInfo.date_hired)
                    item.SubItems.Add(EmployeeInfo.end_contract)
                    item.SubItems.Add(EmployeeInfo.date_resigned)
                End If

                grpEmployeeInfo.Text = "Total Record(s): " + lvwEmployeeInfo.Items.Count.ToString()
                validator.resizes(lvwEmployeeInfo)

            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If
    End Sub

   
    Public Function ClearAllFields()
        validator.ResetControls(grpEmployeeInfo)
        btnEnroll.Enabled = False
        btnEdit.Enabled = False

        validator.resizes(lvwEmployeeInfo)
        lblFPCount.Text = "0"
        Return 0
    End Function

    
    Private Sub lvwEmployeeInfo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwEmployeeInfo.SelectedIndexChanged
        btnEnroll.Enabled = (lvwEmployeeInfo.SelectedItems.Count = 1)
        btnEdit.Enabled = (lvwEmployeeInfo.SelectedItems.Count = 1)
    End Sub


    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Dim empInfoDE As New frmEmployeeInfoDE(EmployeeInfo)
        empInfoDE.ShowDialog()
        frmEmployeeInfoBR_Load(vbNull, EventArgs.Empty)

    End Sub

    Private Sub btnEnroll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnroll.Click
        If MessageBox.Show("All registered fingerprints for this employee will be deleted. Do you want to continue?", _
               "Confirm Operation", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

            Dim employee_id As String = lvwEmployeeInfo.SelectedItems(0).SubItems(1).Text
            bioMgr.deleteAllFingerPrintByEmployeeId(employee_id)
            Dim emp As Employee = empMgr.retrieveEmployeeById(employee_id)
            Dim enroller As New EnrollmentForm(emp)
            enroller.ShowDialog()
            frmEmployeeInfoBR_Load(vbNull, EventArgs.Empty)
        Else

        End If
    End Sub
End Class